package team.you.change.service.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.you.change.dao.ClassMapper;
import team.you.change.dao.RoomMapper;
import team.you.change.pojo.Class;
import team.you.change.util.Result;
import team.you.change.pojo.Room;
import team.you.change.service.RoomService;
import team.you.change.util.YuoUtil;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Resource
    private ClassMapper classMapper;
    @Override
    public Result getAllRooms(Integer page, Integer limit) {
        Result result = new Result();
        List<Room> rooms = roomMapper.selectAll(YuoUtil.getPage(page, limit), limit);
        Integer count = roomMapper.getCount();
        return YuoUtil.setData(result, rooms, count);
    }

    @Override
    public Result del(String id) {
        Result result = new Result();
        Room room = roomMapper.selectByPrimaryKey(id);
        if (room != null) {
            try {
                return YuoUtil.crudMsg(roomMapper.deleteByPrimaryKey(id), result);
            }catch (Exception e){
                return YuoUtil.setErrorMsg(e, result);
            }
        } else {
            result.setCode(500);
            result.setMsg("该行不存在");
        }
        return result;
    }

    @Override
    @Transactional
    public Result delete(String[] ids) {
        Result result = new Result();
        try {
            for (int i = 0; i < ids.length; i++) {
                roomMapper.deleteByPrimaryKey(ids[i]);
            }
            return result;
        } catch (Exception e) {
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    public Result update(Room room) {
        Result result = new Result();
        try {
            return YuoUtil.crudMsg(roomMapper.updateByPrimaryKey(room), result);
        } catch (Exception e) {
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    public Result add(Room room) {
        Result result = new Result();
        String uuid = UUID.randomUUID().toString();
        room.setId(uuid);
        try {
            Result selCode = selCode(room.getRoomCode());
            if(selCode.getCode() == 0){
                return YuoUtil.crudMsg(roomMapper.insert(room), result);
            }
            return selCode;
        }catch (Exception e){
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    public Result selectByLike(String value, Integer type, Integer time, Integer state, Integer page, Integer limit) {
        Result result = new Result();
        List<Room> rooms = roomMapper.selectByLike(value, type, time, state, YuoUtil.getPage(page, limit), limit); //模糊查询时分页
        List<Room> count = roomMapper.selectByLike(value, type, time, state, null, null);
        return YuoUtil.setData(result, rooms, count.size());
    }

    @Override
    public Result selCode(String code) {
        Result result = new Result();
        Room room = roomMapper.selectCode(code);
        if(room == null){
            return result;
        }else {
            result.setCode(500);
            result.setMsg("教室代码重复");
            return result;
        }
    }

    @Override
    public Result state(String id, Integer value, String field) {
        Result result = new Result();
        try{
            return YuoUtil.crudMsg(roomMapper.updateStateById(id, value, field), result);
        }catch (Exception e){
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    public Result getRoomOption(String classId) {
        Result result = new Result();
        Class aClass = classMapper.selectByPrimaryKey(classId);
        if(aClass != null){
            Integer num = aClass.getClassNumber();
            List<Room> rooms = roomMapper.selectAllId(num);
            result.setData(rooms);
            return result;
        }else {
            return YuoUtil.setFlaseMsg(result);
        }
    }
}
